package com.yztc.orangeentertainment.ui.main.findfragment.findmain;

import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lidroid.xutils.exception.DbException;
import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.bean.findbean.FindDbBean;
import com.yztc.orangeentertainment.bean.findbean.SearchListBean;
import com.yztc.orangeentertainment.ui.main.findfragment.findadapter.FindSearchRlvAdapter;
import com.yztc.orangeentertainment.ui.main.findfragment.findpersenter.SearchPresenter;
import com.yztc.orangeentertainment.ui.main.findfragment.findutils.FlowLayout;
import com.yztc.orangeentertainment.ui.main.findfragment.findview.ISearchView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by My on 2016/10/8.
 */
public class FindHotSearchFragment extends Fragment implements ISearchView {
    @InjectView(R.id.findhotsearch_rlv)
    RecyclerView findhotsearchRlv;
    @InjectView(R.id.id_flowlayout)
    FlowLayout mFlowlayout;

    public static TextView footviewText;

    private SearchPresenter presenter;

    public static FindSearchRlvAdapter adapter;
    public static List<FindDbBean> list;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
    Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.findhotsearch, null);
        footviewText = (TextView) view.findViewById(R.id.footview_text);
        ButterKnife.inject(this, view);
        presenter = new SearchPresenter(this);
        presenter.getSearchFromNet(getActivity());
        initData();
        return view;
    }

    public void initData() {
        //初始化数据并设置监听事件
        findhotsearchRlv.setLayoutManager(new LinearLayoutManager(getActivity()) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });

        try {
            list = FindSearchActivity.dbUtils.findAll(FindDbBean.class);
        } catch (DbException e) {
            e.printStackTrace();
        }

        if (list != null && list.size() != 0) {
            adapter = new FindSearchRlvAdapter(getActivity(), list);
            findhotsearchRlv.setAdapter(adapter);
            footviewText.setVisibility(View.VISIBLE);
        }

        footviewText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    FindSearchActivity.dbUtils.deleteAll(FindDbBean.class);
                    FindHotSearchFragment.list.clear();
                    FindHotSearchFragment.adapter.notifyDataSetChanged();
                    footviewText.setVisibility(View.GONE);
                } catch (DbException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void setSearchList(List<SearchListBean> searchListBeens) {
        if(searchListBeens!=null){
            for (int i = 0; i < searchListBeens.size(); i++) {
                final TextView tv = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout
                        .findsearch_flowlayout, mFlowlayout, false);
                tv.setText(searchListBeens.get(i).getKeyword());
                LayerDrawable drawable = (LayerDrawable) tv.getBackground();

                final String str = (String) tv.getText();
                //点击事件
                tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        FindSearchActivity.findsearchEdt.setText(str);
                    }
                });
                mFlowlayout.addView(tv);//添加到父View
            }
        }
    }

    @Override
    public void onVerticalFail(String msg) {

    }

}
